package com.kl.modular.generate.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kl.modular.generate.model.Column;

/**
 * 数据处理
 * 
 * @author zjb
 * @date 2015-4-5
 */
public class GenerateFunctionUtil {

	/**
	 * 数据库数据类型==>实体类数据类型
	 */
	private static Map dataType = new HashMap();

	static {
		dataType.put("varchar", "String");
		dataType.put("int", "int");
		dataType.put("bigint", "Long");
		dataType.put("longtext", "String");
		dataType.put("datetime", "String");
		dataType.put("Date", "Date");
		dataType.put("double", "double");
		dataType.put("bigint", "Long");
		dataType.put("decimal", "BigDecimal");
		dataType.put("timestamp", "Date");
		dataType.put("tinyint", "Long");
		dataType.put("mediumint", "Long");
		dataType.put("bit", "Boolean");
		dataType.put("text", "String");
		dataType.put("date", "Date");
		dataType.put("char", "String");
	}

	/**
	 * 数据表字段信息 TO 实体类信息
	 * 
	 * @param columnList
	 * @return
	 */
	public static List<Column> dataColumnToProperty(List<Column> columnList) {
		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			// 处理实体类数据类型
			System.out.println("--------------"+(column.getDataType())
					);
			column.setPropertyDataType((dataType.get(column.getDataType())
					.toString()));
			// 处理实体类属性名称
			String columnName = column.getColumnName();
			String propertyName = "";
			if (columnName.contains("_")) {
				String[] columnNames = columnName.split("_");
				propertyName += columnNames[0];
				for (int j = 1; j < columnNames.length; j++) {
					String columnPre = columnNames[j].substring(0, 1);
					String columnSuf = columnNames[j].substring(1,
							columnNames[j].length());
					String newColumnName = columnPre.toUpperCase() + columnSuf;
					propertyName += newColumnName;
				}
			} else {
				propertyName = columnName;
			}
			column.setPropertyName(propertyName);
			// 处理get set 方法名
			String gsPre = propertyName.substring(0, 1);
			String gsSuf = propertyName.substring(1, propertyName.length());
			String newGsMethodName = gsPre.toUpperCase() + gsSuf;
			column.setGsPropertyName(newGsMethodName);
			columnList.set(i, column);
		}
		return columnList;
	}

	/**
	 * 数据表名 TO 实体类名
	 * 
	 * @param tableName
	 * @return
	 */
	public static String tableNameToModelName(String tableName) {
		String modelName = "";
		if (tableName.contains("_")) {
			String[] columnNames = tableName.split("_");
			modelName += columnNames[0];
			for (int j = 1; j < columnNames.length; j++) {
				String columnPre = columnNames[j].substring(0, 1);
				String columnSuf = columnNames[j].substring(1, columnNames[j]
						.length());
				String newColumnName = columnPre.toUpperCase() + columnSuf;
				modelName += newColumnName;
			}
		} else {
			modelName = tableName;
		}
		String gsPre = modelName.substring(0, 1);
		String gsSuf = modelName.substring(1, modelName.length());
		modelName = gsPre.toUpperCase() + gsSuf;
		return modelName;
	}

	/**
	 * 变量首字母转为
	 * 
	 * @param variable
	 *            名称
	 * @return
	 */
	public static String firstLetterToLopper(String variable) {
		String firstLetter = variable.substring(0, 1);
		String otherLetter = variable.substring(1, variable.length());
		variable = firstLetter.toLowerCase() + otherLetter;
		return variable;
	}

	/**
	 * 获取表中主键值
	 * 
	 * @param columnList
	 *            字段集合
	 * @return
	 */
	public static Column getTablePriKey(List<Column> columnList) {
		Column columnPriKey = new Column();
		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			if (column.getColumnKey().equals("PRI")) {
				columnPriKey = column;
				break;
			}
		}
		return columnPriKey;
	}

}
